package com.bmadmin.controller.sys;

import com.bmadmin.domain.NormalResult;
import com.bmadmin.domain.SysDepartmentsPOJO;
import com.bmadmin.service.SysDepartmentService;
import com.bmadmin.util.BMUtil;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by SEELE on 2017/12/3.
 */
@RestController
@RequestMapping("/departments")
public class SysDepartmentsController {
    private Logger logger = LogManager.getLogger(this.getClass().getName());
    @Autowired
    SysDepartmentService sysDepartmentService;

    /**
     * 获取部门列表树
     * @param sysDepartmentsPOJO
     * @return
     */
    @GetMapping("/getTreeList")
    public NormalResult getDepartmentTreeList(SysDepartmentsPOJO sysDepartmentsPOJO){
        NormalResult result = sysDepartmentService.getDepartmentByTree(sysDepartmentsPOJO);
        logger.info(result);
        return result;
    }

    /**
     * 新增部门
     */
    @PostMapping("/insert")
    public NormalResult insertDepartment(SysDepartmentsPOJO sysDepartmentsPOJO){
        NormalResult result = new NormalResult();
        if(BMUtil.isNullOrEmpty(sysDepartmentsPOJO)){
            result.setCode("9999");
            result.setMsg("传入数据为空");
            return result;
        }
        if("".equals(BMUtil.NulltoString(sysDepartmentsPOJO.getD_id()))){
            result.setCode("9999");
            result.setMsg("传入部门id为空");
            return result;
        }
        if("".equals(BMUtil.NulltoString(sysDepartmentsPOJO.getD_name()))){
            result.setCode("9999");
            result.setMsg("传入部门名称为空");
            return result;
        }
        if("".equals(BMUtil.NulltoString(sysDepartmentsPOJO.getD_part_id()))){
            result.setCode("9999");
            result.setMsg("传入上级部门id为空");
            return result;
        }

        result = sysDepartmentService.insertDepartment(sysDepartmentsPOJO);

        return result;
    }

    /**
     * 删除部门
     */
    @PostMapping("/delete")
    public NormalResult deleteDepartment(SysDepartmentsPOJO sysDepartmentsPOJO){
        NormalResult result = new NormalResult();
        if(BMUtil.isNullOrEmpty(sysDepartmentsPOJO)){
            result.setCode("9999");
            result.setMsg("传入数据为空");
            return result;
        }
        if("".equals(BMUtil.NulltoString(sysDepartmentsPOJO.getD_id()))){
            result.setCode("9999");
            result.setMsg("传入部门id为空");
            return result;
        }

        result = sysDepartmentService.delDepartment(sysDepartmentsPOJO);
        return result;

    }

    /**
     * 获取部门信息
     */
    @GetMapping("/getById")
    public NormalResult getDepartmentById(SysDepartmentsPOJO sysDepartmentsPOJO){
        NormalResult result = new NormalResult();
        if(BMUtil.isNullOrEmpty(sysDepartmentsPOJO)){
            result.setCode("9999");
            result.setMsg("传入数据为空");
            return result;
        }
        if("".equals(BMUtil.NulltoString(sysDepartmentsPOJO.getD_id()))){
            result.setCode("9999");
            result.setMsg("传入部门id为空");
            return result;
        }

        result=sysDepartmentService.getDepartmentById(sysDepartmentsPOJO);
        return result;

    }
    /**
     * 修改部门
     */
    @PostMapping("/update")
    public NormalResult updateDepartment(SysDepartmentsPOJO sysDepartmentsPOJO){
        NormalResult result = new NormalResult();
        if(BMUtil.isNullOrEmpty(sysDepartmentsPOJO)){
            result.setCode("9999");
            result.setMsg("传入数据为空");
            return result;
        }
        if("".equals(BMUtil.NulltoString(sysDepartmentsPOJO.getD_id()))){
            result.setCode("9999");
            result.setMsg("传入部门id为空");
            return result;
        }
        if("".equals(BMUtil.NulltoString(sysDepartmentsPOJO.getD_name()))){
            result.setCode("9999");
            result.setMsg("传入部门名称为空");
            return result;
        }

        result = sysDepartmentService.updateDepartment(sysDepartmentsPOJO);

        return result;
    }


}
